package com.wanczy.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.wanczy.pojo.Admin;
import com.wanczy.pojo.GoodsAndShop;
import com.wanczy.pojo.Shop;
import com.wanczy.pojo.User;
import com.wanczy.service.AdminService;

@SessionAttributes(value="admin")
@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;
	
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String findByPassAndName(@RequestParam("adminName")String adminName,@RequestParam("adminPass") String adminPass,Map<String,Object> map) {
		Admin admin = adminService.findByPassAndName(adminName, adminPass);
		if(null != admin){
			map.put("admin", admin);
			return "/admin/body";
		}
		return "/login/fail";
	}
	
	@ResponseBody
	@RequestMapping(value="/findUser",method=RequestMethod.GET)
	public List<User> findByNameLike(@RequestParam("userName")String userName) {
		try {
			userName = URLDecoder.decode(userName,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<User> list = adminService.findByNameLike(userName);
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/findShop",method=RequestMethod.GET)
	public List<Shop> findByShopName(@RequestParam("shopName")String shopName) {
		try {
			shopName = URLDecoder.decode(shopName,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<Shop> list = adminService.findByShopName(shopName);
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/doUpdShop")
	public List<Boolean> doUpdShop(@RequestParam("shopId")Integer shopId,@RequestParam("shopState")Integer shopState) {
		boolean flag = adminService.updShop(shopId, shopState);
		List<Boolean> list = new ArrayList<Boolean>();
		list.add(flag);
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/doUpdUser")
	public List<Boolean> doUpdUser(@RequestParam("userId")Integer userId,@RequestParam("userState")Integer userState) {
		boolean flag = adminService.updUser(userId, userState);
		List<Boolean> list = new ArrayList<Boolean>();
		list.add(flag);
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/findByGoodsName",method=RequestMethod.GET)
	public List<GoodsAndShop> findByGoodsName(@RequestParam("goodsName")String goodsName) {
		try {
			goodsName = URLDecoder.decode(goodsName,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<GoodsAndShop> list = adminService.findByGoodsName(goodsName);
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/doUpdGoods")
	public List<Boolean> doUpdGoods(@RequestParam("goodsId")Integer goodsId,@RequestParam("goodsState")Integer goodsState) {
		boolean flag = adminService.updGoods(goodsId, goodsState);
		List<Boolean> list = new ArrayList<Boolean>();
		list.add(flag);
		return list;
	}
	
}
